package com;

import com.Mapper.BrandMapper;
import com.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MybatisDemo2 {
    @Test
    public void testSelectAll() throws IOException {
        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brands = brandMapper.selectAll();

        System.out.println(brands);

        sqlSession.close();


    }

    @Test
    public void testSelectById() throws IOException {
        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        Brand brand = brandMapper.selectById(3);

        System.out.println(brand);

        sqlSession.close();


    }

    @Test
    public void testSelectByCondition1() throws IOException {
        //接收参数
        int brandStatus =1;
        String companyName="小";
        String brandName="小";

        //处理参数
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brand = brandMapper.selectByCondition(brandStatus, companyName, brandName);
        System.out.println(brand);


        sqlSession.close();


    }

    @Test
    public void testSelectByCondition2() throws IOException {
        //接收参数
        int brandStatus =1;
        String companyName="小";
        String brandName="小";

        //处理参数
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";

        //封装对象
        Brand brand1 = new Brand();
        brand1.setBrandStatus(brandStatus);
        brand1.setCompanyName(companyName);
        brand1.setBrandName(brandName);



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brand =brandMapper.selectByCondition(brand1);
        System.out.println(brand);

        sqlSession.close();


    }

    @Test
    public void testSelectByCondition3() throws IOException {
        //接收参数
        int brandStatus =1;
        String companyName="小";
        String brandName="小";

        //处理参数
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";


        Map map = new HashMap();
        //map.put("brandStatus",brandStatus);
        map.put("companyName",companyName);
        //map.put("brandName",brandName);


        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brand =brandMapper.selectByCondition(map);
        System.out.println(brand);

        sqlSession.close();


    }

    @Test
    public void testSelectByConditionSingle() throws IOException {
        //接收参数
        int brandStatus =1;
        String companyName="小";
        String brandName="小";

        //处理参数
        companyName = "%"+companyName+"%";
        brandName = "%"+brandName+"%";



        //封装对象
        Brand brand1 = new Brand();
        brand1.setBrandStatus(brandStatus);
        //brand1.setCompanyName(companyName);
        //brand1.setBrandName(brandName);



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> brand =brandMapper.selectByCondition(brand1);
        System.out.println(brand);

        sqlSession.close();


    }

    @Test
    public void testAdd() throws IOException {
        //接收参数
        int brandStatus =6;
        String companyName="米哈游有限公司";
        String brandName="米哈游";
        String description="一定是米哈游干的";
        int orderd =11111;
        //int id =5;



        //封装对象
        Brand brand1 = new Brand();
        brand1.setBrandStatus(brandStatus);
        brand1.setCompanyName(companyName);
        brand1.setBrandName(brandName);
        brand1.setDescription(description);
        brand1.setOrderd(orderd);
        //brand1.setId(id);



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();//这里可以把autoCommit设置为true,则后文不需加commit语句
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        brandMapper.add(brand1);



        //手动提交事务
        sqlSession.commit();





        System.out.println(brand1.getId());

        sqlSession.close();


    }

    @Test
    public void testUpdate() throws IOException {
        //接收参数
        int brandStatus =9;
        String companyName="白象公司";
        String brandName="白象";
        String description="国货之光";
        int orderd =1234556;

        int id =9;



        //封装对象
        Brand brand1 = new Brand();
        brand1.setBrandStatus(brandStatus);
        brand1.setCompanyName(companyName);
        brand1.setBrandName(brandName);
        brand1.setDescription(description);
        brand1.setOrderd(orderd);
        brand1.setId(id);



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();//这里可以把autoCommit设置为true,则后文不需加commit语句
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int count = brandMapper.update(brand1);



        //手动提交事务
        sqlSession.commit();





        System.out.println(count);

        sqlSession.close();


    }

    @Test
    public void testUpdateDymanically() throws IOException {
        //接收参数
        int brandStatus =9;
        String companyName="白象公司";
        String brandName="白象";
/*        String description="国货之光";
        int orderd =1234556;*/

        int id =10;



        //封装对象
        Brand brand1 = new Brand();
        brand1.setBrandStatus(brandStatus);
        brand1.setCompanyName(companyName);
        brand1.setBrandName(brandName);
        /*brand1.setDescription(description);
        brand1.setOrderd(orderd);*/
        brand1.setId(id);



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();//这里可以把autoCommit设置为true,则后文不需加commit语句
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);
        int count = brandMapper.updateDynamically(brand1);



        //手动提交事务
        sqlSession.commit();





        System.out.println(count);

        sqlSession.close();


    }
    @Test
    public void testDeleteById() throws IOException {
        //接收参数


        int id =11;



        //封装对象
/*        Brand brand1 = new Brand();
        brand1.setBrandStatus(brandStatus);
        brand1.setCompanyName(companyName);
        brand1.setBrandName(brandName);
        brand1.setDescription(description);
        brand1.setOrderd(orderd);
        brand1.setId(id);*/



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();//这里可以把autoCommit设置为true,则后文不需加commit语句
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        brandMapper.deleteById(id);



        //手动提交事务
        sqlSession.commit();





        /*System.out.println(count);*/

        sqlSession.close();


    }


    @Test
    public void testDeleteByIds() throws IOException {
        //接收参数


        int[] ids ={1,3,5,6,9};



        //封装对象
/*        Brand brand1 = new Brand();
        brand1.setBrandStatus(brandStatus);
        brand1.setCompanyName(companyName);
        brand1.setBrandName(brandName);
        brand1.setDescription(description);
        brand1.setOrderd(orderd);
        brand1.setId(id);*/



        //获取工厂对象
        String resource = "mybatis-config.xml";
        InputStream resourceAsStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //用工厂对象获取直接执行sql的session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();//这里可以把autoCommit设置为true,则后文不需加commit语句
        //使用mapper代理执行sql
        BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);

        brandMapper.deleteByIds(ids);



        //手动提交事务
        sqlSession.commit();





        /*System.out.println(count);*/

        sqlSession.close();


    }





}
